package com.tontron.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.tontron.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    private Logger logger = LoggerFactory.getLogger(MyMetaObjectHandler.class);;

    @Override
    public void insertFill(MetaObject metaObject) {
        logger.info("开始插入填充...");
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());

        //TODO 超级管理创建租户账号时不需要自动插入值
        this.strictInsertFill(metaObject, "tenantId", Long.class, SecurityUtils.getLoginUser().getUser().getTenantId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        logger.info("开始更新填充...");
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }
}
